import { Player } from "../player.entity";
import { Effect } from "./effect";
import { PushService } from "../../services/push_service";
import { PushRoute } from "../../consts/push_route";
import { Hurt } from "../hurt";
import { AttackState } from "../../consts/attack_state";
import { EffectType } from "../../consts/effect_type";
import { Unit } from "../unit";
import { UnitType } from "../../consts/unit_type";

export class Attack implements Effect{
    private _active:Unit = null;
    private _target:Unit = null;
    private _isRun:Boolean=true;
    private _canAttack:Boolean=true;

 
    getName():EffectType{
        return EffectType.Attack;
    }
    getActive(): Unit {
        return this._active;
    }
    getTarget(): Unit {
        return this._target;
    }
    isRun(){
        return this._isRun;
    }
    setCanAttack(bool){
        this._canAttack=bool;
    }


    constructor(active:Unit,target:Unit) {
        this._active = active;
        this._target = target;
    }

    public static getHurt(active:Unit,target:Unit):Hurt{
        let attackState={hurt:active.attack,attackState:AttackState.Hit};
        return attackState;
    }


    run(): void {
        if(!this._isRun||!this._canAttack)return;
        if (this._active.attackOverTime > Date.now()){
            return;
        }
        let hurt=Attack.getHurt(this._active,this._target);
        this._target.setCurHp(this._target.getCurHp()-hurt.hurt);
        console.log("effect.attack 释放者'"+this._active.unitType+"'攻击力="+this._active.attack+",攻击状态="+hurt.attackState.toString()+",目标'"+this._target.unitType+"'血量="+this._target.getCurHp()+"/"+this._target.maxHp);
        this._canAttack=false;
        let pushId=this._target.pushId?this._target.pushId:this._active.pushId;
        PushService.ins().pushToOne(PushRoute.FightOnAttack,"fight",pushId,PushRoute.FightOnAttackMsg(this._active.unitId,
            this._target.unitId,hurt));
         this._active.attackOverTime = Date.now() + this._active.attackCD;
    }

    cancel(){
        this._isRun=false;
    }
    
     
}